﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: HYF
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using FriendStar.Code;
using FriendStar.Domain.Entity.BaseData;
using FriendStar.Domain.IRepository.SystemManage;
using FriendStar.Repository.BaseData;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using System.IO;
using System.Drawing;
using System;
using Gma.QrCodeNet.Encoding;
using ZXing.Common;
using ZXing;
using System.Drawing.Imaging;

namespace FriendStar.Application.SystemManage
{
    public class DutyApp
    {
        private IRoleRepository service = new RoleRepository();

        public List<RoleEntity> GetList(string keyword = "")
        {
            var expression = ExtLinq.True<RoleEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_FullName.Contains(keyword));
                expression = expression.Or(t => t.F_EnCode.Contains(keyword));
            }
            expression = expression.And(t => t.F_Category == 2);
            return service.IQueryable(expression).OrderBy(t => t.F_SortCode).ToList();
        }
        public RoleEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(RoleEntity roleEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                roleEntity.Modify(keyValue);
                service.Update(roleEntity);
            }
            else
            {
                roleEntity.Create();
                roleEntity.F_Category = 2;
                service.Insert(roleEntity);
            }
        }

        public byte[] GetQrcode(string text)
        {
            byte[] result = null;

            QrEncoder Encoder = null;

            try
            {
                 Encoder = new QrEncoder(ErrorCorrectionLevel.H);

                QrCode Code = Encoder.Encode(text);

                Bitmap bmp = new Bitmap(Code.Matrix.Width, Code.Matrix.Height);

                for (int X = 0; X <= Code.Matrix.Width - 1; X++)
                {
                    for (int Y = 0; Y <= Code.Matrix.Height - 1; Y++)
                    {
                        if (Code.Matrix.InternalArray[X, Y])
                            bmp.SetPixel(X, Y, Color.Black);
                        else
                            bmp.SetPixel(X, Y, Color.White);
                    }
                }

                using (MemoryStream mostream = new MemoryStream())
                {
                    bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图像以指定的格式存入缓存内存流

                    result = new byte[mostream.Length];

                    mostream.Position = 0;//设置留的初始位置

                    mostream.Read(result, 0, Convert.ToInt32(result.Length));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return result;

        }

        public Bitmap GetBarCode(string text,int width,int height)
        {
            EncodingOptions encodeOption = new EncodingOptions();

            encodeOption.Height = height; // 必须制定高度、宽度

            encodeOption.Width = width;
            // 2.生成条形码图片并保存
            BarcodeWriter wr = new BarcodeWriter();

            wr.Options = encodeOption;

            wr.Format = BarcodeFormat.CODE_128; //  条形码规格：EAN13规格：12（无校验位）或13位数字

            return wr.Write(text); // 生成图片
        }
    }
}
